<?php
namespace App\Domain;
use App\Model\User as Model_User;
use App\Model\Auth as Model_Auth;
use App\Domain\Vip as Domain_Vip;
use App\Domain\Gift as Domain_Gift;
use App\Domain\Wall as Domain_Wall;
use App\Domain\Label as Domain_Label;
use function PhalApi\DI;

class User {
	/* 设置头像、昵称、性别 */
    public function setUserInfo($uid,$avatar,$name,$sex,$country='',$language='') {
        
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());

        $data=[];
        if($avatar){
            $avatar_q=\App\get_upload_path($avatar);
            $configpri=\App\getConfigPri();
            $cloudtype=$configpri['cloudtype'];
            if($cloudtype==1){
                 $avatar=  $avatar_q.'?imageView2/2/w/600/h/600'; //600 X 600
				 $avatar_thumb=  $avatar_q.'?imageView2/2/w/200/h/200'; // 200 X 200
            }else{
                $avatar=$avatar_q;
                $avatar_thumb=$avatar_q;
            }
            $data['avatar']=$avatar;
            $data['avatar_thumb']=$avatar_thumb;
        }
        else {
            if($sex == 1){
                $img = mt_rand(1,200);
                $avatar = 'man/'.$img.'.png';
                $avatar_thumb = 'man/'.$img.'.png';
            }else{
                $img = mt_rand(1,200);
                $avatar = 'woman/'.$img.'.png';
                $avatar_thumb = 'woman/'.$img.'.png';
            }
            $data['avatar']=$avatar;
            $data['avatar_thumb']=$avatar_thumb;
        }

        $model = new Model_User();
        if($name){
			if($name==\PhalApi\T('用户已注销') || $name==\PhalApi\T('已注销')){
                $rs['code'] = 1003;
                $rs['msg'] = \PhalApi\T('请填写正确的昵称');
                return $rs;
            }
			
            $count=mb_strlen($name);
            if($count>7){
                $rs['code'] = 1004;
                $rs['msg'] = \PhalApi\T('昵称最多7个字');
                return $rs;
            }
            
            /*$isexist = $model->checkNickname($uid,$name);
            if($isexist){
                $rs['code'] = 1003;
                $rs['msg'] = '昵称已存在';
                return $rs;
            }*/
            
            $data['user_nickname']=$name;
        }
		$data['sex']=$sex;
        $data['country'] = $country;
        $data['language'] = $language;
        if(!$data){
            $rs['code'] = 1005;
            $rs['msg'] = \PhalApi\T('信息错误');
            return $rs; 
        }
        
        $result = $model->upUserInfo($uid,$data);
        
        \App\hMSet("userinfo_".$uid,$data);
        
        $userinfo = $model->getBaseInfo($uid);
        
        $info=[
            'avatar'=>\App\get_upload_path($userinfo['avatar']),
            'avatar_thumb'=>\App\get_upload_path($userinfo['avatar_thumb']),
            'user_nickname'=>$userinfo['user_nickname'],
			'sex'=>$userinfo['sex'],
        ];
        $rs['info'][0]=$info;
        return $rs;
    }

    /* 用户基本信息 */
    public function getBaseInfo($uid) {
        
        $model = new Model_User();
        $info = $model->getBaseInfo($uid);
        
        if($info){
            $info['avatar']=\App\get_upload_path($info['avatar']);
            $info['avatar_thumb']=\App\get_upload_path($info['avatar_thumb']);						
            $info['level']=\App\getLevel($info['consumption']);
            //ggg
            if(empty($info['level'])){
                $info['level'] = 0;
            }
            unset($info['consumption']);
            $info['level_anchor']=\App\getLevelanchor($info['votestotal']);
            /* $info['level_anchor']=getLevelanchor($info['goodnums'],$info['badnums']); */
            unset($info['goodnums']);
            unset($info['badnums']);
            $info['follows']=\App\getFollows($uid);
            $fans = \App\getFans($uid);;
//            if($info['isauthor_auth']){
//                $fans=\App\getFans($uid);
//            }
            $info['fans']=$fans;
            
            /* VIP */
            $domain_vip = new Domain_Vip();
            $vipinfo=$domain_vip->myVip($info['id']);
            $info['isvip']=$vipinfo['isvip'];

            $likes=$model->getLikeNums($uid);
            $info['likes']=(string)$likes;

            unset($info['isauth']);
            unset($info['isauthor_auth']);
        }

        return $info;
    }
    
    /* 语音价格 */
    public function getVoice() {
        $rs = array();

        $key='fee_voicelist';
        $list=\App\getcaches($key);
        if(!$list){
            $model = new Model_User();
            $list = $model->getVoice();
            if($list){
                \App\setcaches($key,$list);
            }
        }

        return $list;
    }
    /* 视频价格 */
    public function getVideo() {
        $rs = array();

        $key='fee_videolist';
        $list=\App\getcaches($key);
        if(!$list){
            $model = new Model_User();
            $list = $model->getVideo();
            if($list){
                \App\setcaches($key,$list);
            }
        }

        return $list;
    }
    
    /* 语音开关 */
    public function setVoiceSwitch($uid,$isvoice) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('语音接听已开启'), 'info' => array());
        
        $isvoice=$isvoice ? 1 : 0;

        $data=[
            'isvoice'=>$isvoice
        ];
        $model = new Model_User();
        $info = $model->upUserInfo($uid,$data);
        if($isvoice==0){
            $rs['msg']=\PhalApi\T('语音接听已关闭');
        }
        
        return $rs;
    }
    
    /* 语音价格 */
    public function setVoiceValue($uid,$value) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        $isok=0;
        $list=$this->getVoice();
        foreach($list as $k=>$v){
            if($v['coin']==$value){
                $isok=1;
                break;
            }
        }
        
        if(!$isok){
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('信息错误');
            return $rs;
        }
        
        $data=[
            'voice_value'=>$value
        ];
        $model = new Model_User();
        $info = $model->upUserInfo($uid,$data);

        return $rs;
    }
    
    /* 视频开关 */
    public function setVideoSwitch($uid,$isvideo) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('视频接听已开启'), 'info' => array());
        
        $isvideo=$isvideo ? 1 : 0;

        $data=[
            'isvideo'=>$isvideo
        ];
        $model = new Model_User();
        $info = $model->upUserInfo($uid,$data);
        if($isvideo==0){
            $rs['msg']=\PhalApi\T('视频接听已关闭');
        }
        
        return $rs;
    }
    
    /* 视频价格 */
    public function setVideoValue($uid,$value) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        /*$isok=0;
        $list=$this->getVideo();
        foreach($list as $k=>$v){
            if($v['coin']==$value){
                $isok=1;
                break;
            }
        }
        
        if(!$isok){
            $rs['code'] = 1002;
            $rs['msg'] = \PhalApi\T('信息错误');
            return $rs;
        }*/
        
        $data=[
            'video_value'=>$value
        ];
        $model = new Model_User();
        $info = $model->upUserInfo($uid,$data);

        return $rs;
    }
    
    
    /* 勿扰开关 */
    public function setDisturbSwitch($uid,$isdisturb) {

        $rs = array('code' => 0, 'msg' => \PhalApi\T('勿扰模式已开启'), 'info' => array());
        
        $isdisturb=$isdisturb ? 1 : 0;

        $data=[
            'isdisturb'=>$isdisturb
        ];
        $data['online']=3;
        if($isdisturb==1){
            $data['online']=1;
        }
        $model = new Model_User();
        $info = $model->upUserInfo($uid,$data);
        
        if($isdisturb==0){
            $rs['msg']=\PhalApi\T('勿扰模式已关闭');
        }
        
        return $rs;
    }
    
    
    /* 更新用户信息 */
    public function upUserInfo($uid,$post_data) {
        
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());

        $labelid='';

        if(isset($post_data['labelid'])){
            $labelid=$post_data['labelid'];
        }

        $model = new Model_User();


        if($labelid){

            $list_kv=[];
            $label_list=$this->getLabelList();
            foreach($label_list as $k=>$v){
                $list_kv[$v['id']]=$v;
            }

            $label=[];
            $label_c=[];
            $isok=1;

            $label_a=array_filter( preg_split('/,|，/', $labelid) );

            foreach($label_a as $k=>$v){
                $labelinfo=$list_kv[$v];
                if(!$labelinfo){
                    $isok=0;
                    break;
                }
                
                $labelids[]=$labelinfo['id'];
                $label[]=$labelinfo['name'];
                $label_c[]=$labelinfo['colour'];
            }

            if(!$isok){
                $rs['code'] = 1014;
                $rs['msg'] = \PhalApi\T('形象标签信息错误');
                return $rs;
            }

            if(!$label){
                $rs['code'] = 1015;
                $rs['msg'] = \PhalApi\T('形象标签信息错误');
                return $rs;
            }

            $post_data['labelid']=implode(',',$labelids);
            $post_data['label']=implode(',',$label);
            $post_data['label_c']=implode(',',$label_c);

        }

        $old_userinfo=$model->getBaseInfo($uid);
        //file_put_contents("bbbb.txt",$uid.'提交用户信息'.json_encode($post_data)."\r\n",FILE_APPEND);

        $result = $model->upUserInfo($uid,$post_data);

        if($result===false){
            $rs['code'] = 1016;
            $rs['msg'] = \PhalApi\T('资料编辑失败');
            return $rs;
        }

        $configpri=\App\getConfigPri();

        
        //file_put_contents("bbbb.txt",$uid.'查询的用户信息'.json_encode($old_userinfo)."\r\n",FILE_APPEND);

        if(isset($post_data['avatar'])){

            $coin=$configpri['up_avatar_coin'];

            if($coin){

                $user_task_info = $model->getUserTask($uid,'up_avatar');

                if(!$user_task_info){

                    if($old_userinfo['avatar'] != $post_data['avatar']){

                        //file_put_contents("bbbb.txt",$uid.'提交的用户头像跟原头像不一致'."\r\n",FILE_APPEND);

                        \App\addCoin($uid,$coin);

                        $inset_data=[
                            'type'=>1,
                            'action'=>13, //新手任务-上传头像
                            'uid'=>$uid,
                            'touid'=>$uid,
                            'totalcoin'=>$coin,
                            'addtime'=>time()
                        ];

                        \App\addCoinRecord($inset_data);

                        $model->changeUserTask($uid,'up_avatar');
                    }

                   
                }

                
            }
            
            
        }

        if(
            isset($post_data['sex']) ||
            isset($post_data['audio']) ||
            isset($post_data['height']) ||
            isset($post_data['weight']) ||
            isset($post_data['constellation']) ||
            isset($post_data['label']) ||
            isset($post_data['city']) ||
            isset($post_data['intr']) ||
            isset($post_data['signature'])
        ){

            file_put_contents("aaaa.txt", $uid.'进入更新用户信息'."\r\n",FILE_APPEND);

            $coin=$configpri['update_info_coin'];

            if($coin){

                $user_task_info = $model->getUserTask($uid,'update_info');

                if(!$user_task_info){

                    $is_update_info=0;

                    if(isset($post_data['sex']) && $post_data['sex'] != $old_userinfo['sex'] ){
                        $is_update_info=1;
                    }

                    if(isset($post_data['audio']) && $post_data['audio'] != $old_userinfo['audio'] ){
                        $is_update_info=1;
                    }

                    if(isset($post_data['height']) && $post_data['height'] != $old_userinfo['height'] ){
                        $is_update_info=1;
                    }

                    if(isset($post_data['weight']) && $post_data['weight'] != $old_userinfo['weight'] ){
                        $is_update_info=1;
                    }

                    if(isset($post_data['constellation']) && $post_data['constellation'] != $old_userinfo['constellation'] ){
                        $is_update_info=1;
                    }

                    if(isset($post_data['label']) && $post_data['label'] != $old_userinfo['label'] ){
                        $is_update_info=1;
                    }

                    if(isset($post_data['city']) && $post_data['city'] != $old_userinfo['city'] ){
                        $is_update_info=1;
                    }

                    if(isset($post_data['intr']) && $post_data['intr'] != $old_userinfo['intr'] ){
                        $is_update_info=1;
                    }

                    if(isset($post_data['signature']) && $post_data['signature'] != $old_userinfo['signature'] ){
                        $is_update_info=1;
                    }

                    if($is_update_info){
                        \App\addCoin($uid,$coin);

                        $inset_data=[
                            'type'=>1,
                            'action'=>14, //新手任务-完善资料
                            'uid'=>$uid,
                            'touid'=>$uid,
                            'totalcoin'=>$coin,
                            'addtime'=>time()
                        ];

                        \App\addCoinRecord($inset_data);

                        $model->changeUserTask($uid,'update_info');
                    }

                    
                }

                
            }

        }
        
        \App\hMSet("userinfo_".$uid,$post_data);
        
        $userinfo=$model->getBaseInfo($uid);
        
        $label_list=[];

        $label_a=array_filter( preg_split('/,|，/', $userinfo['labelid']) );
        $label_name_a=array_filter( preg_split('/,|，/', $userinfo['label']) );
        $label_color_a=array_filter( preg_split('/,|，/', $userinfo['label_c']) );

        foreach ($label_a as $k => $v) {
           $arr=[];
           $arr['id']=$v; 
           $arr['name']=$label_name_a[$k];
           $arr['colour']=$label_color_a[$k];

           $label_list[]=$arr;
        }
        
        $info=[
            'avatar'=>\App\get_upload_path($userinfo['avatar']),
            'avatar_thumb'=>\App\get_upload_path($userinfo['avatar_thumb']),
            'user_nickname'=>$userinfo['user_nickname'],
            'audio'=>$userinfo['audio'],
            'audio_length'=>$userinfo['audio_length'],
            'audio_format'=>\App\get_upload_path($userinfo['audio']),
            'sex'=>$userinfo['sex'],
            'height'=>$userinfo['height'],
            'weight'=>$userinfo['weight'],
            'constellation'=>$userinfo['constellation'],
            'label_list'=>$label_list,
            'province'=>$userinfo['province'],
            'city'=>$userinfo['city'],
            'district'=>$userinfo['district'],
            'intr'=>$userinfo['intr'],
            'address'=>$userinfo['address'],
        ];


        $rs['info'][0]=$info;
        return $rs;
    }


    /* 形象标签列表 */
    public function getLabelList() {

        $key='labellist';
        //ggg
        $list=\App\getcaches($key);
        //$list = '';
        if(!$list){
            $model = new Model_User();
            $list = $model->getLabelList();
            if($list){
                \App\setcaches($key,$list);
            }
        }
        return $list;
    }


    /* 关注列表 */
    public function getFollowsList($uid,$p) {
        $rs = array();

        $model = new Model_User();
        
        $domain_vip = new Domain_Vip();
        
        $rs = $model->getFollowsList($uid,$p);
        foreach($rs as $k=>$v){
			$userinfo=\App\getUserInfo($v['touid']);
            $userinfo['fans']=\App\getFans($userinfo['id']);
            
            /* VIP */
            $vipinfo=$domain_vip->myVip($userinfo['id']);
            $userinfo['isvip']=$vipinfo['isvip'];
            
            $rs[$k]=$userinfo;
		}


        return $rs;
    }    
    /* 粉丝列表 */
    public function getFansList($uid,$p) {
        $rs = array();

        $model = new Model_User();
        
        $domain_vip = new Domain_Vip();
        
        $rs = $model->getFansList($uid,$p);
        foreach($rs as $k=>$v){
			$userinfo=\App\getUserInfo($v['uid']);
            $usercoin=\App\getUserCoin($v['uid']);
            $userinfo['isauth']= \App\isAuthorAuth($v['uid']);
            $userinfo['u2t']= \App\isAttention($uid,$v['uid']);
            $userinfo['coin']='0';
            if($usercoin){
                $userinfo['coin']=$usercoin['coin'];
            }
            $userinfo['coin']=number_format($userinfo['coin']);
			$userinfo['fans']=\App\getFans($userinfo['id']);
            
            /* VIP */
            $vipinfo=$domain_vip->myVip($userinfo['id']);
            $userinfo['isvip']=$vipinfo['isvip'];
			$userinfo['isblack']=\App\isBlack($uid,$v['uid']);
            
            $rs[$k]=$userinfo;
		}

        return $rs;
    }
    /* 关注、取消关注 */
    public function setAttent($uid,$touid) {
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        
        if($uid == $touid){
			$rs['code']=1001;
			$rs['msg']=\PhalApi\T('不能关注自己');
			return $rs;
		}
        
        $where="id = {$touid}";
        $isexist=\App\checkUser($where);
        
        if(!$isexist){
            $rs['code']=1001;
			$rs['msg']=\PhalApi\T('关注用户不存在');
			return $rs;
        }
        
        $model = new Model_User();
        
        $isAttention=\App\isAttention($uid,$touid);
        
        if($isAttention){
            /* 已关注 */
            $model->delAttent($uid,$touid);
            $rs['info'][0]['isattent']='0';
        }else{
            /* 未关注 */
            $model->setAttent($uid,$touid);
            $rs['info'][0]['isattent']='1';
        }

        return $rs;
	}

    /* 粉丝列表 */
    public function getBlackList($uid,$p) {
        $rs = array();

        $model = new Model_User();
        
        $domain_vip = new Domain_Vip();
        
        $rs = $model->getBlackList($uid,$p);
        foreach($rs as $k=>$v){
			$userinfo=\App\getUserInfo($v['touid']);
            $rs[$k]=$userinfo;
		}

        return $rs;
    }
    
    /* 拉黑、取消拉黑 */
    public function setBlack($uid,$touid) {
        $rs = array('code' => 0, 'msg' => \PhalApi\T('操作成功'), 'info' => array());
        
        if($uid == $touid){
			$rs['code']=1001;
			$rs['msg']=\PhalApi\T('不能拉黑自己');
			return $rs;
		}
        
        $where="id = {$touid}";
        $isexist=\App\checkUser($where);
        
        if(!$isexist){
            $rs['code']=1001;
			$rs['msg']=\PhalApi\T('拉黑用户不存在');
			return $rs;
        }
        
        $model = new Model_User();
        
        $isBlack=\App\isBlack($uid,$touid);
        
        if($isBlack){
            /* 已拉黑 */
            $model->delBlack($uid,$touid);
            $rs['info'][0]['isblack']='0';
        }else{
            /* 未拉黑 */
            $model->setBlack($uid,$touid);
            $rs['info'][0]['isblack']='1';
        }
        $isAttention=\App\isAttention($uid,$touid);
        if($isAttention){
            /* 已关注 */
            $model->delAttent($uid,$touid);
        }
        return $rs;
	}

    /* 更新位置 */
    public function setLocal($uid,$lng,$lat) {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $data=[
            'lng'=>$lng,
            'lat'=>$lat,
        ];
        $model = new Model_User();

        $rs=$model->upUserInfo($uid,$data);


        return $rs;
	}

    /* 礼物柜 */
    public function getGiftCab($liveuid) {
        $rs = array();

        $model = new Model_User();

        $rs=$model->getGiftCab($liveuid);
        
        if($rs['nums']>0){
            
            $gift_list=[];
            $domain = new Domain_Gift();
            $giftlist = $domain->getGiftList();
            foreach($giftlist as $k=>$v){
                $gift_list[$v['id']]=$v;
            }

            $list=$rs['list'];
            if($list){
                foreach($list as $k=>$v){
                    $v['thumb']=\App\get_upload_path('/logo.png');
                    $v['name']=\PhalApi\T('礼物已删除');
                    $giftinfo=$gift_list[$v['actionid']];
                    if($giftinfo){
                        $v['thumb']=$giftinfo['thumb'];
                        $v['name']=$giftinfo['name'];
                    }
                    $list[$k]=$v;
                }
                $rs['list']=$list;
            }
        }

        return $rs;
	}

    /* 个人主页 */
    public function getUserHome($uid,$liveuid) {
        $info = array();

        $model = new Model_User();

        $userinfo=$model->getUserHome($liveuid);
        if($userinfo){
            $userinfo['avatar']=\App\get_upload_path($userinfo['avatar']);
            $userinfo['avatar_thumb']=\App\get_upload_path($userinfo['avatar_thumb']);
            $userinfo['audio']=\App\get_upload_path($userinfo['audio']); 

            $userinfo['level_anchor']=\App\getLevelanchor($userinfo['votestotal']);
            /* $userinfo['level_anchor']=getLevelanchor($userinfo['goodnums'],$userinfo['badnums']); */

            $fans=\App\getFans($liveuid);
            $userinfo['fans']=$fans;
            //ggg
            $userinfo['level'] = \App\getLevel($userinfo['consumption']);
        }
        
        $answer_rate = $this->getAnswerRate($liveuid);
        
        /* 认证信息 */
        $model_auth = new Model_Auth();
		$authinfo = $model_auth->getAuthorAuthInfo($liveuid);
        if ($authinfo) {
            unset($authinfo['id']);
            $info=array_merge($userinfo,$authinfo);
        }else {
            $info = $userinfo;
        }


        
        /* 背景墙 封面 */
        $domain_wall = new Domain_Wall();
        $wallinfo=$domain_wall->myWall($liveuid);
        
        $info['thumb']=$wallinfo['thumb'];
        $info['photos_list']=$wallinfo['photos'];
        
        $info['isattent']=\App\isAttention($uid,$liveuid);
        $info['answer_rate']=$answer_rate;
        if($info['online']==3){
            $info['last_online_time']=\PhalApi\T('在线');
        }else if($info['online']==2){
            $info['last_online_time']=\PhalApi\T('在聊');
        }else if($info['online']==1){
            $info['last_online_time']=\PhalApi\T('勿扰');
        }else{
            $offtime=\App\offtime($info['last_online_time']);
            $info['last_online_time']=$offtime;
        }

        $info['video']=\App\get_upload_path($info['video']);
        $info['video_thumb']=\App\get_upload_path($info['video_thumb']);
        
        /* if($info['isdisturb']==1){
            $info['online']='1';
            $info['last_online_time']='勿扰';
        } */
        
        /* 礼物柜 */
        $giftlist=$this->getGiftCab($liveuid);
        $info['gift_total']=$giftlist['nums'];
        $info['gift_list']=array_slice($giftlist['list'],0,5);
        /* 用户印象 */
        $domain_label = new Domain_Label();
		$labellist = $domain_label->getEvaluateCount($liveuid);
        
        $info['evaluate_list']=array_slice($labellist,0,3);

        
        if($info['label']){
            $label_a=explode(',',$info['label']);
            $label_c_a=explode(',',$info['label_c']);
            $label=[];
            foreach($label_a as $k=>$v){
                $label[]=[
                    'name'=>$v,
                    'colour'=>$label_c_a[$k],
                ];
            }
            
            $info['label_list']=$label;
        }
        
        unset($info['uid']);
        unset($info['reason']);
        unset($info['addtime']);
        unset($info['uptime']);
        unset($info['status']);
        unset($info['labelid']);
        unset($info['label']);
        unset($info['label_c']);

        $info['sex']=(string)$info['sex'];
        
        /* VIP */
        $domain_vip = new Domain_Vip();
        $vipinfo=$domain_vip->myVip($liveuid);
        $info['isvip']=$vipinfo['isvip'];
        
        $info['isblack']=\App\isBlack($uid,$liveuid);

        // 获取直播等级信息
        $level = DI()->notorm->level->where("level=" . $info['level'])->order('id desc')->fetchOne();
        if ($level && !empty($level['thumb'])) {
            $level['thumb'] = \App\get_upload_path($level['thumb']);
        }
        // 获取用户等级
        $levelAuthor = DI()->notorm->level_anchor->where("level=" . $info['level_anchor'])->order('id desc')->fetchOne();
        if ($levelAuthor && !empty($levelAuthor['thumb'])) {
            $levelAuthor['thumb'] = \App\get_upload_path($levelAuthor['thumb']);
        }
        $info['levelData'] = $level;
        $info['levelAnchorData'] = $levelAuthor;


        return $info;
	}

    /* 接听率 */
    public function getAnswerRate($liveuid) {
        $info = array();

        $rate='0';
        $model = new Model_User();

        $info=$model->getAnswerRate($liveuid);

        if($info){
            if($info['answer'] && $info['total']){
                $rate=floor($info['answer']/$info['total']*100);
                if($rate>100){
                    $rate=100;
                }
            }
            
        }
        return $rate.'%';
	}
	
	
	//用户举报-分类
	public function getReportClass() {
		$rs = array();
		$key='getUserReportClassify';
        $list=\App\getcaches($key);
        if(!$list){
            $model = new Model_User();
			$list = $model->getReportClass();
            if($list){
                \App\setcaches($key,$list);
            }
        }
		
		$lang=\PhalApi\DI()->lang;
		foreach($list as $k=>$v){
			if($lang=='en'){
				$v['name']=$v['name_en'];
			}
			$list[$k]=$v;
		}
		
		
		return $list;
	}
	
	//用户举报
	public function setReport($uid,$touid,$content) {
		$rs = array();

		$model = new Model_User();
		$rs = $model->setReport($uid,$touid,$content);
		return $rs;
	}
	
	//美颜预设
	public function setBeauty($uid,$type,$preinstall){
		$rs = array();

		$model = new Model_User();
		$rs = $model->setBeauty($uid,$type,$preinstall);
		return $rs;
	}
	
	
	//获取美颜预设
	public function getBeauty($uid){
		$rs = array();

		$model = new Model_User();
		$rs = $model->getBeauty($uid);
		
		
		return $rs;
	}
	
	//推荐用户信息
	public function getRecommendlist($p,$uid=''){
		$rs = array();

		$model = new Model_User();
		$rs = $model->getRecommendlist($p,$uid);
		return $rs;
	}
	//获取打招呼文案信息
	public function getSayhelloinfo(){
		$model = new Model_User();
		$rs = $model->getSayhelloinfo();
		return $rs;
	}
	//获取用户列表
	public function getUserList($uid,$p,$type){
		$rs = array();

		$model = new Model_User();
		$rs = $model->getUserList($uid,$p,$type);
		return $rs;
	}

    //
    public function getUserMaterial($uid){

        $model = new Model_User();
        $info = $model->getUserMaterial($uid);
        if($info){
            $info['avatar']=\App\get_upload_path($info['avatar']);
            $info['avatar_thumb']=\App\get_upload_path($info['avatar_thumb']);
            $info['audio']=\App\get_upload_path($info['audio']);

            if($info['height']==0){
                $info['height']='';
            }

            if($info['weight']==0){
                $info['weight']='';
            }

            $labelid_a=[];
            if($info['labelid']){
                $labelid_a=explode(',',$info['labelid']);
                $label_a=explode(',',$info['label']);
                $label_c_a=explode(',',$info['label_c']);
            }
            

            $label=[];

            if(!empty($labelid_a)){

                foreach ($labelid_a as $k => $v) {
                    //var_dump($v);exit;
                    $label[]=[
                        'id'=>$v,
                        'name'=>$label_a[$k],
                        'colour'=>$label_c_a[$k],
                        'name_en'=>\PhalApi\DI()->notorm->label->where('id',$v)->fetchOne()['name_en']
                    ];
                }
            }
            
            $info['label_list']=$label;
            $info['name']=\App\htmlSpecialCharsDecode($info['name']);
            $info['intr']=\App\htmlSpecialCharsDecode($info['intr']);
            $info['signature']=\App\htmlSpecialCharsDecode($info['signature']);

            $info['city'] = htmlspecialchars_decode(htmlspecialchars_decode($info['city']));

            unset($info['labelid']);
            unset($info['label']);
            unset($info['label_c']);
        }

        return $info;

    }


    public function checkTeenager($uid){
        $rs = array();

        $model = new Model_User();
        $rs = $model->checkTeenager($uid);

        return $rs;
    }

    public function checkTeenagerIsOvertime($uid){
        $rs = array();
        $model = new Model_User();
        $rs = $model->checkTeenagerIsOvertime($uid);
        return $rs;
    }

    public function setTeenagerPassword($uid,$password,$type){
        $rs = array();

        $model = new Model_User();
        $rs = $model->setTeenagerPassword($uid,$password,$type);

        return $rs;
    }

    public function updateTeenagerPassword($uid,$oldpassword,$password){
        $rs = array();

        $model = new Model_User();
        $rs = $model->updateTeenagerPassword($uid,$oldpassword,$password);

        return $rs;
    }

    public function closeTeenager($uid,$password){
        $rs = array();

        $model = new Model_User();
        $rs = $model->closeTeenager($uid,$password);

        return $rs;
    }

    public function addTeenagerTime($uid){
        $rs = array();

        $model = new Model_User();
        $rs = $model->addTeenagerTime($uid);

        return $rs;
    }

    public function LoginBonus($uid){
        $rs = array();
        $model = new Model_User();
        $rs = $model->LoginBonus($uid);
        return $rs;

    }

    public function getLoginBonus($uid){
        $rs = array();
        $model = new Model_User();
        $rs = $model->getLoginBonus($uid);
        return $rs;

    }

    public function getTaskList($uid){

        $rs = array();
        $model = new Model_User();
        //$type_list=['up_avatar','user_auth','anchor_auth','update_info'];
        $type_list=['up_avatar','user_auth','update_info'];
        $user_task_title=\PhalApi\T('新手任务');
        $user_task_list=[];

        $configpub=\App\getConfigpub();
        $configpri=\App\getConfigPri();

        foreach ($type_list as $k => $v) {

            $status='0';
            $coin_str='+'.$configpri[$v.'_coin'].$configpub['name_coin'];

            if($v=='up_avatar'){
                $title=\PhalApi\T('上传头像');
                $desc_str=\PhalApi\T('用最帅最美的照片做头像');
                
            }else if($v=='user_auth'){
                $title=\PhalApi\T('实名认证');
                $desc_str=\PhalApi\T('真人认证，回复率更高');

            }/*else if($v=='anchor_auth'){
                $title=\PhalApi\T('主播认证');
                $desc_str=\PhalApi\T('完成认证，解锁更多玩法');

            }*/else if($v=='update_info'){
                $title=\PhalApi\T('完善资料');
                $desc_str=\PhalApi\T('让TA更了解你');
            }

            $task_info = $model->getUserTask($uid,$v);

            if($task_info){
                $coin_str='+'.$task_info['coin'].$configpub['name_coin'];
                $status='1';
            }

            $arr=[
                'title'=>$title,
                'desc_str'=>$desc_str,
                'coin_str'=>$coin_str,
                'status'=>$status,
                'sign'=>$v
            ];

            $user_task_list[]=$arr;
        }


        $rs['user_task_title']=$user_task_title;
        $rs['user_task_list']=$user_task_list;


        $daily_task_title=\PhalApi\T('每日任务');
        $daily_task_list=[];

        $dailytask_switch=$configpri['dailytask_switch'];

        if($dailytask_switch){


            $type_list=[
                'daily_signin',
                'publish_dynamic',
                'like_dynamic',
                'send_privatemsg',
                //'voice_call',
                'video_call',
                'open_live'
            ];

            $isauth=\App\isAuthorAuth($uid);

            foreach ($type_list as $k => $v) {
                $status='0';

                if($v=='daily_signin'){
                    $login_bonus = $model->LoginBonus($uid);
                    //未开启每日任务
                    if(!$login_bonus['bonus_switch']){
                        continue;
                    }

                    //登录天数 0表示已奖励
                    if(!$login_bonus['bonus_day']){
                       $status='2';
                    }

                    $coin_str='+'.$login_bonus['coin'].$configpub['name_coin'];

                }else{

                    //var_dump($v.'_coin');

                    $coin_str='+'.$configpri[$v.'_coin'].$configpub['name_coin'];
                }

                if($v=='daily_signin'){
                    $title=\PhalApi\T('每日签到');
                    $desc_str=\PhalApi\T('每日打卡签到，可获得丰厚奖励');

                }else{

                    $dailytask_check=$model->checkDailyTasks($uid,$v);
                    $status=(string)$dailytask_check['state'];

                    if($v=='publish_dynamic'){
                        $title=\PhalApi\T('发布动态');
                        $desc_str=\PhalApi\T('每日至少发布{v_term}个动态',['v_term'=>$configpri[$v.'_term']]);

                    }else if($v=='like_dynamic'){
                        $title=\PhalApi\T('点赞动态');
                        $desc_str=\PhalApi\T('每日至少点赞{v_term}个动态',['v_term'=>$configpri[$v.'_term']]);
                    }else if($v=='send_privatemsg'){
                        $title=\PhalApi\T('发送私信');
                        $desc_str=\PhalApi\T('私聊心仪对象每日不少于{v_term}个',['v_term'=>$configpri[$v.'_term']]);
                    }else if($v=='voice_call'){
                        $title=\PhalApi\T('语音通话');
                        $desc_str=\PhalApi\T('每日开启语音通话不低于{v_term}分钟',['v_term'=>$configpri[$v.'_term']]);
                    }else if($v=='video_call'){
                        $title=\PhalApi\T('视频通话');
                        $desc_str=\PhalApi\T('每日开启视频通话不低于{v_term}分钟',['v_term'=>$configpri[$v.'_term']]);
                    }else if($v=='open_live'){
                        $title=\PhalApi\T('开启直播');
                        $desc_str=\PhalApi\T('每日开启直播不低于{v_term}分钟',['v_term'=>$configpri[$v.'_term']]);
                    }

                }

                if(!$isauth && ($v=='publish_dynamic' || $v=='open_live')){
                    continue;
                }

                $arr=[
                    'title'=>$title,
                    'desc_str'=>$desc_str,
                    'coin_str'=>$coin_str,
                    'status'=>$status,
                    'sign'=>$v
                ];

                $daily_task_list[]=$arr;
                
            }
        }

        $rs['daily_task_title']=$daily_task_title;
        $rs['daily_task_list']=$daily_task_list;

        return $rs;
    }

    public function receiveTaskReward($uid,$type){
        $rs=[];
        $model=new Model_User();
        $rs=$model->receiveTaskReward($uid,$type);

        return $rs;
    }

    //守护榜单
    public function guardList($uid,$type,$p){
        switch ($type) {
            case 'day':
                //获取今天开始结束时间
                $dayStart=strtotime(date("Y-m-d"));
                $dayEnd=strtotime(date("Y-m-d 23:59:59"));
                $where=" addtime >={$dayStart} and addtime<={$dayEnd} and ";

            break;
            
            case 'week':
                $w=date('w'); 
                //获取本周开始日期，如果$w是0，则表示周日，减去 6 天 
                $first=1;
                //周一
                $week=date('Y-m-d H:i:s',strtotime( date("Ymd")."-".($w ? $w - $first : 6).' days')); 
                $week_start=strtotime( date("Ymd")."-".($w ? $w - $first : 6).' days'); 

                //本周结束日期 
                //周天
                $week_end=strtotime("{$week} +1 week")-1;
                
                $where=" addtime >={$week_start} and addtime<={$week_end} and ";

            break;

            case 'month':
                //本月第一天
                $month=date('Y-m-d',strtotime(date("Ym").'01'));
                $month_start=strtotime(date("Ym").'01');

                //本月最后一天
                $month_end=strtotime("{$month} +1 month")-1;

                $where=" addtime >={$month_start} and addtime<={$month_end} and ";

            break;

            case 'total':
                $where=" ";
            break;
            
            default:
                //获取今天开始结束时间
                $dayStart=strtotime(date("Y-m-d"));
                $dayEnd=strtotime(date("Y-m-d 23:59:59"));
                $where=" addtime >={$dayStart} and addtime<={$dayEnd} and ";
            break;
        }

        $where.=" type='0' and action='1'";
        
        $model = new Model_User();
        $result=$model->guardList($where,$p);
        
        foreach ($result as $k => $v) {
            $userinfo=\App\getUserInfo($v['uid']);
            $v['uid']=(string)$v['uid'];
            $v['avatar']=$userinfo['avatar'];
            $v['avatar_thumb']=$userinfo['avatar_thumb'];
            $v['user_nickname']=$userinfo['user_nickname'];
            $v['sex']=(string)$userinfo['sex'];
            $v['level']=$userinfo['level'];
            $v['level_anchor']=$userinfo['level_anchor'];
            
            $v['isAttention']=\App\isAttention($uid,$v['uid']);//判断当前用户是否关注了该主播
            $v['isAuth']=\App\isAuth($v['uid']);//判断当前用户是否认证
            $result[$k]=$v;
        }

        return $result;
    }


    public function getRandomSayhi($uid){
        $rs = array();

        $model = new Model_User();
        $rs = $model->getRandomSayhi($uid);

        return $rs;
    }

    public function addFirstSayhiRecord($uid,$touid){
        $rs = array();

        $model = new Model_User();
        $rs = $model->addFirstSayhiRecord($uid,$touid);

        return $rs;
    }


    public function getlikeList($uid,$p) {
        $rs = array();

        $model = new Model_User();
        
        $domain_vip = new Domain_Vip();
        
        $rs = $model->getlikeList($uid,$p);
        foreach($rs as $k=>$v){
            $userinfo=\App\getUserInfo($v['uid']);
            $usercoin=\App\getUserCoin($v['uid']);
            $userinfo['isauth']= \App\isAuthorAuth($v['uid']);
            $userinfo['u2t']= \App\isAttention($uid,$v['uid']);
            $userinfo['coin']='0';
            if($usercoin){
                $userinfo['coin']=$usercoin['coin'];
            }
            $userinfo['coin']=number_format($userinfo['coin']);
            $userinfo['fans']=\App\getFans($userinfo['id']);
            
            /* VIP */
            $vipinfo=$domain_vip->myVip($userinfo['id']);
            $userinfo['isvip']=$vipinfo['isvip'];
            $userinfo['isblack']=\App\isBlack($uid,$v['uid']);
            
            $rs[$k]=$userinfo;
        }

        return $rs;
    }


	
}
